package Admin;

import JDBCutil.JDBCUtil;
import bean.Course;
import bean.Student;
import bean.Teacher;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import static GetBound.getbound.getBound;
import static JDBCutil.JDBCUtil.*;

/*
 * Created by JFormDesigner on Wed Aug 07 16:01:00 CST 2024
 */



/**
 * @author 29454
 */
public class Admin extends JFrame {
    public Admin() {
        initComponents();
        setBounds(getBound());
        setExtendedState(JFrame.MAXIMIZED_BOTH);
    }

    public static int[] getSelectedid(JTable table){
        int[] selected = table.getSelectedRows();
        int[] ids = new int[selected.length];
        for (int i = 0; i < selected.length; i++) {
            int rowindex=selected[i];
            Object idobj=table.getValueAt(rowindex,0);
            ids[i] = Integer.parseInt(idobj.toString());
        }
        return ids;
    }

    public static String[] getSelectedNum(JTable table){
        int[] selected = table.getSelectedRows();
        String[] ids = new String[selected.length];
        for (int i = 0; i < selected.length; i++) {
            int rowindex=selected[i];
            Object idobj1=table.getValueAt(rowindex,0);
            Object idobj=table.getValueAt(rowindex,1);
            Object idobj3=table.getValueAt(rowindex,2);
            String num=idobj1.toString()+idobj.toString()+idobj3.toString();
            ids[i] = num;
        }
        return ids;
    }



    //       自适应屏幕
//    public static Rectangle getBound(){
//        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
//        Insets scr=Toolkit.getDefaultToolkit().getScreenInsets(new JFrame().getGraphicsConfiguration());
//        Rectangle rectangle = new Rectangle(scr.left,scr.top,screenSize.width-scr.left-scr.right,
//                screenSize.height-scr.top-scr.bottom);
//        return rectangle;
//    }

    private void showstu(ActionEvent e) throws Exception {
        // TODO add your code here
        // TODO add your code here
        JPanel panel = (JPanel) scrollPane1.getViewport().getView();
        if (panel != null) {
            panel.removeAll();  // 清空面板所有组件
            panel.revalidate();  // 重新验证面板布局
            panel.repaint();  // 重新绘制面板}
        }

        scrollPane1.getViewport().revalidate();  // 重新验证视口
        scrollPane1.getViewport().repaint();  // 重新绘制视口
        Showstu showstu = new Showstu();
        showstu.setVisible(true);
        scrollPane1.setViewportView(showstu);

    }

    private void addstu(ActionEvent e) {
        // TODO add your code here
        AddStu addStu = new AddStu(this);
    }

    private void modify(ActionEvent e) throws Exception {
        // TODO add your code here
        ModifyStu modifyStu = new ModifyStu(this);

    }

    private void DelStu(ActionEvent e) throws Exception {
        // TODO add your code here

            int[] ids = getSelectedid(Showstu.getTable1());
            deleteStudent(ids);
            System.out.println(0);

    }

    private void select(ActionEvent e) throws Exception {
        // TODO add your code here
        String s=textField1.getText();
        int id = Integer.parseInt(s);
        if(s.length()==5){
            Student stu=JDBCUtil.getStudent(id);
            JPanel panel = (JPanel) scrollPane1.getViewport().getView();
            if (panel != null) {
                panel.removeAll();  // 清空面板所有组件
                panel.revalidate();  // 重新验证面板布局
                panel.repaint();  // 重新绘制面板}
            }

            scrollPane1.getViewport().revalidate();  // 重新验证视口
            scrollPane1.getViewport().repaint();  // 重新绘制视口
            ShowsingleStu showstu = new ShowsingleStu(stu);
            showstu.setVisible(true);
            scrollPane1.setViewportView(showstu);
        }
        if(s.length()==4){
            Teacher t=JDBCUtil.getSTeacher(id);
            JPanel panel = (JPanel) scrollPane1.getViewport().getView();
            if (panel != null) {
                panel.removeAll();  // 清空面板所有组件
                panel.revalidate();  // 重新验证面板布局
                panel.repaint();  // 重新绘制面板}
            }

            scrollPane1.getViewport().revalidate();  // 重新验证视口
            scrollPane1.getViewport().repaint();  // 重新绘制视口
            ShowsingleTeacher showsingleTeacher = new ShowsingleTeacher(t);
            showsingleTeacher.setVisible(true);
            scrollPane1.setViewportView(showsingleTeacher);
        }
        if(s.length()==1){
            Course c=JDBCUtil.getCourse(id);
            JPanel panel = (JPanel) scrollPane1.getViewport().getView();
            if (panel != null) {
                panel.removeAll();  // 清空面板所有组件
                panel.revalidate();  // 重新验证面板布局
                panel.repaint();  // 重新绘制面板}
            }

            scrollPane1.getViewport().revalidate();  // 重新验证视口
            scrollPane1.getViewport().repaint();  // 重新绘制视口
            ShowsingleCourse sc = new ShowsingleCourse(c);
            sc.setVisible(true);
            scrollPane1.setViewportView(sc);
        }

    }

    private void ShowAllTeacher(ActionEvent e) throws Exception {
        // TODO add your code here
        JPanel panel = (JPanel) scrollPane1.getViewport().getView();
        if (panel != null) {
            panel.removeAll();  // 清空面板所有组件
            panel.revalidate();  // 重新验证面板布局
            panel.repaint();  // 重新绘制面板}
        }

        scrollPane1.getViewport().revalidate();  // 重新验证视口
        scrollPane1.getViewport().repaint();  // 重新绘制视口
        ShowTeacher showTeacher = new ShowTeacher();
        showTeacher.setVisible(true);
        scrollPane1.setViewportView(showTeacher);
    }

    private void addteacher(ActionEvent e) {
        // TODO add your code here
        AddTeacher addTeacher = new AddTeacher(this);
    }

    private void modfiyteacher(ActionEvent e) throws Exception {
        // TODO add your code here
        ModifyTeacher modifyTeacher = new ModifyTeacher(this);
    }

    private void delTeacher(ActionEvent e) throws Exception {
        // TODO add your code here
        try {
            int[] ids = getSelectedid(ShowTeacher.getTable1());
            deleteTeacher(ids);
            System.out.println(0);
        }
        catch (Exception e1) {e1.printStackTrace();}
    }

    private void showcourese(ActionEvent e) {
        // TODO add your code here
        JPanel panel = (JPanel) scrollPane1.getViewport().getView();
        if (panel != null) {
            panel.removeAll();  // 清空面板所有组件
            panel.revalidate();  // 重新验证面板布局
            panel.repaint();  // 重新绘制面板}
        }

        scrollPane1.getViewport().revalidate();  // 重新验证视口
        scrollPane1.getViewport().repaint();  // 重新绘制视口
        try {
            ShowCourse showc = new ShowCourse();
            showc.setVisible(true);
            scrollPane1.setViewportView(showc);
        }
        catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    private void addcourse(ActionEvent e) {
        // TODO add your code here
        AddCourse addCourse = new AddCourse(this);
    }

    private void modifycourse(ActionEvent e) throws Exception {
        // TODO add your code here
        ModifyCourse modifyCourse = new ModifyCourse(this);
    }

    private void delCourse(ActionEvent e) {
        // TODO add your code here
        try {
            int[] ids = getSelectedid(ShowCourse.getTable1());
            deleteCourse(ids);
            System.out.println(0);
        }
        catch (Exception e1) {e1.printStackTrace();}
    }

    private void Showselect(ActionEvent e) {
        // TODO add your code here
        JPanel panel = (JPanel) scrollPane1.getViewport().getView();
        if (panel != null) {
            panel.removeAll();  // 清空面板所有组件
            panel.revalidate();  // 重新验证面板布局
            panel.repaint();  // 重新绘制面板}
        }

        scrollPane1.getViewport().revalidate();  // 重新验证视口
        scrollPane1.getViewport().repaint();  // 重新绘制视口
        try {
            ShowSelectCourse showc = new ShowSelectCourse();
            showc.setVisible(true);
            scrollPane1.setViewportView(showc);
        }
        catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    private void addselect(ActionEvent e) {
        // TODO add your code here
        AddSelectCourse selectCourse = new AddSelectCourse(this);
    }

    private void modifyselect(ActionEvent e) throws Exception {
        // TODO add your code here
        ModifySelecctCourse selecctCourse=new ModifySelecctCourse(this);
    }

    private void delcetselect(ActionEvent e) {
        // TODO add your code here
        try {
            String[] ids = getSelectedNum(ShowSelectCourse.getTable1());
            deleteSelectCourse(ids);
            System.out.println(0);
        }
        catch (Exception e1) {e1.printStackTrace();}

    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        menuBar1 = new JMenuBar();
        menu1 = new JMenu();
        menuItem1 = new JMenuItem();
        menuItem2 = new JMenuItem();
        menuItem3 = new JMenuItem();
        menuItem10 = new JMenuItem();
        menu2 = new JMenu();
        menuItem4 = new JMenuItem();
        menuItem5 = new JMenuItem();
        menuItem6 = new JMenuItem();
        menuItem11 = new JMenuItem();
        menu3 = new JMenu();
        menuItem7 = new JMenuItem();
        menuItem8 = new JMenuItem();
        menuItem9 = new JMenuItem();
        menuItem12 = new JMenuItem();
        menu4 = new JMenu();
        menuItem13 = new JMenuItem();
        menuItem14 = new JMenuItem();
        menuItem15 = new JMenuItem();
        menuItem16 = new JMenuItem();
        textField1 = new JTextField();
        button1 = new JButton();
        scrollPane1 = new JScrollPane();

        //======== this ========
        setTitle("\u7ba1\u7406\u5458");
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== menuBar1 ========
        {

            //======== menu1 ========
            {
                menu1.setText("\u5b66\u751f\u7ba1\u7406");
                menu1.addActionListener(e -> {try {
showstu(e);} catch (Exception ex) {
    throw new RuntimeException(ex);
}});

                //---- menuItem1 ----
                menuItem1.setText("\u67e5\u770b\u5b66\u751f\u4fe1\u606f");
                menuItem1.addActionListener(e -> {try {
showstu(e);} catch (Exception ex) {
    throw new RuntimeException(ex);
}});
                menu1.add(menuItem1);

                //---- menuItem2 ----
                menuItem2.setText("\u6dfb\u52a0\u5b66\u751f\u4fe1\u606f");
                menuItem2.addActionListener(e -> addstu(e));
                menu1.add(menuItem2);

                //---- menuItem3 ----
                menuItem3.setText("\u4fee\u6539\u5b66\u751f\u4fe1\u606f");
                menuItem3.addActionListener(e -> {try {
modify(e);} catch (Exception ex) {
    throw new RuntimeException(ex);
}});
                menu1.add(menuItem3);

                //---- menuItem10 ----
                menuItem10.setText("\u5220\u9664\u5b66\u751f\u4fe1\u606f");
                menuItem10.addActionListener(e -> {try {
DelStu(e);} catch (Exception ex) {
    throw new RuntimeException(ex);
}});
                menu1.add(menuItem10);
            }
            menuBar1.add(menu1);

            //======== menu2 ========
            {
                menu2.setText("\u6559\u5e08\u7ba1\u7406");

                //---- menuItem4 ----
                menuItem4.setText("\u67e5\u770b\u6559\u5e08\u4fe1\u606f");
                menuItem4.addActionListener(e -> {try {
ShowAllTeacher(e);} catch (Exception ex) {
    throw new RuntimeException(ex);
}});
                menu2.add(menuItem4);

                //---- menuItem5 ----
                menuItem5.setText("\u6dfb\u52a0\u6559\u5e08\u4fe1\u606f");
                menuItem5.addActionListener(e -> addteacher(e));
                menu2.add(menuItem5);

                //---- menuItem6 ----
                menuItem6.setText("\u4fee\u6539\u6559\u5e08\u4fe1\u606f");
                menuItem6.addActionListener(e -> {try {
modfiyteacher(e);} catch (Exception ex) {
    throw new RuntimeException(ex);
}});
                menu2.add(menuItem6);

                //---- menuItem11 ----
                menuItem11.setText("\u5220\u9664\u6559\u5e08\u4fe1\u606f");
                menuItem11.addActionListener(e -> {try {
delTeacher(e);} catch (Exception ex) {
    throw new RuntimeException(ex);
}});
                menu2.add(menuItem11);
            }
            menuBar1.add(menu2);

            //======== menu3 ========
            {
                menu3.setText("\u8bfe\u7a0b\u7ba1\u7406");

                //---- menuItem7 ----
                menuItem7.setText("\u67e5\u770b\u8bfe\u7a0b\u4fe1\u606f");
                menuItem7.addActionListener(e -> showcourese(e));
                menu3.add(menuItem7);

                //---- menuItem8 ----
                menuItem8.setText("\u6dfb\u52a0\u8bfe\u7a0b\u4fe1\u606f");
                menuItem8.addActionListener(e -> addcourse(e));
                menu3.add(menuItem8);

                //---- menuItem9 ----
                menuItem9.setText("\u4fee\u6539\u8bfe\u7a0b\u4fe1\u606f");
                menuItem9.addActionListener(e -> {try {
modifycourse(e);} catch (Exception ex) {
    throw new RuntimeException(ex);
}});
                menu3.add(menuItem9);

                //---- menuItem12 ----
                menuItem12.setText("\u5220\u9664\u8bfe\u7a0b\u4fe1\u606f");
                menuItem12.addActionListener(e -> delCourse(e));
                menu3.add(menuItem12);
            }
            menuBar1.add(menu3);

            //======== menu4 ========
            {
                menu4.setText("\u9009\u8bfe\u7ba1\u7406");

                //---- menuItem13 ----
                menuItem13.setText("\u67e5\u770b\u9009\u8bfe\u4fe1\u606f");
                menuItem13.addActionListener(e -> Showselect(e));
                menu4.add(menuItem13);

                //---- menuItem14 ----
                menuItem14.setText("\u6dfb\u52a0\u9009\u8bfe\u4fe1\u606f");
                menuItem14.addActionListener(e -> addselect(e));
                menu4.add(menuItem14);

                //---- menuItem15 ----
                menuItem15.setText("\u4fee\u6539\u9009\u8bfe\u4fe1\u606f");
                menuItem15.addActionListener(e -> {try {
modifyselect(e);} catch (Exception ex) {
    throw new RuntimeException(ex);
}});
                menu4.add(menuItem15);

                //---- menuItem16 ----
                menuItem16.setText("\u5220\u9664\u9009\u8bfe\u4fe1\u606f");
                menuItem16.addActionListener(e -> delcetselect(e));
                menu4.add(menuItem16);
            }
            menuBar1.add(menu4);
            menuBar1.add(textField1);

            //---- button1 ----
            button1.setText("\u641c\u7d22");
            button1.addActionListener(e -> {try {
select(e);} catch (Exception ex) {
    throw new RuntimeException(ex);
}});
            menuBar1.add(button1);
        }
        setJMenuBar(menuBar1);
        contentPane.add(scrollPane1,BorderLayout.CENTER);
        scrollPane1.setBounds(0, 0, 1700, 850);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(825, 455);
        setLocationRelativeTo(null);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JMenuBar menuBar1;
    private JMenu menu1;
    private JMenuItem menuItem1;
    private JMenuItem menuItem2;
    private JMenuItem menuItem3;
    private JMenuItem menuItem10;
    private JMenu menu2;
    private JMenuItem menuItem4;
    private JMenuItem menuItem5;
    private JMenuItem menuItem6;
    private JMenuItem menuItem11;
    private JMenu menu3;
    private JMenuItem menuItem7;
    private JMenuItem menuItem8;
    private JMenuItem menuItem9;
    private JMenuItem menuItem12;
    private JMenu menu4;
    private JMenuItem menuItem13;
    private JMenuItem menuItem14;
    private JMenuItem menuItem15;
    private JMenuItem menuItem16;
    private JTextField textField1;
    private JButton button1;
    private JScrollPane scrollPane1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
